package com.zxbg.controller.proportalController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.Process;
import com.zxbg.service.proportalService.ProcessCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/processCenter")
public class ProcessCenterController {
    @Autowired
    ProcessCenterService processCenterService;

    @PostMapping("/submitForm")
    public Result submitForm(@RequestBody Map<String, Object> submitData, @RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        String app_id = (String) submitData.get("app_id");
        Object formData = submitData.get("formData");
        Integer mode = Integer.parseInt(String.valueOf(submitData.get("mode")));
        // 判断是否是已保存的草稿
        Integer process_id = -1;
        if(submitData.containsKey("process_id")) {
            process_id = Integer.parseInt(String.valueOf(submitData.get("process_id")));
        }
        String res = processCenterService.submitForm(app_id, formData, mode, employee_id, tenant_id, process_id);
        return new Result(res.equals("") ? Code.SAVE_OK : Code.SAVE_ERR, res, res);
    }

    @PostMapping("/getTargetProcessInfo")
    public Result getTargetProcessInfo(@RequestBody Map<String, String> search_info, @RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        String status = "";
        String app_id = "";
        if(search_info.containsKey("status")) {
            status = search_info.get("status");
        }
        if(search_info.containsKey("app_id")) {
            app_id = search_info.get("app_id");
        }
        String mode = search_info.get("mode");
        List<Process> ProcessList = processCenterService.getTargetProcessInfo(search_info.get("key"), mode, employee_id, tenant_id, status, app_id);
        List<Process> resList = new ArrayList<>();
        Integer startNum = (Integer.parseInt(search_info.get("current_page")) - 1) * Integer.parseInt(search_info.get("page_size"));
        Integer endNum = Integer.parseInt(search_info.get("current_page")) * Integer.parseInt(search_info.get("page_size")) - 1;
        for (int i = 0; i < ProcessList.size(); i++) {
            Process process = ProcessList.get(i);
            if(i >= startNum && i <= endNum) {
                resList.add(process);
            }
        }
        Map<String,Object> first_map = new HashMap<>();
        Map<String,Object> second_map = new HashMap<>();
        second_map.put("page_size", Integer.parseInt(search_info.get("page_size")));
        second_map.put("current_page", Integer.parseInt(search_info.get("current_page")));
        second_map.put("total", ProcessList.size());
        first_map.put("page", second_map);
        first_map.put("rows", resList);
        if(mode.equals("my_apply")) {
            List<Process> allProcess = processCenterService.getTargetProcessInfo(search_info.get("key"), mode, employee_id, tenant_id, "all", app_id);
            Integer[] typeNumbers = new Integer[] {allProcess.size(), 0, 0, 0};
            for (Process process : allProcess) {
                String process_status = process.getProcess_status();
                if(process_status.equals("inApproval")) {
                    typeNumbers[1] += 1;
                } else if(process_status.equals("end")) {
                    typeNumbers[2] += 1;
                } else if(process_status.equals("termination")) {
                    typeNumbers[3] += 1;
                }
            }
            first_map.put("numbers", typeNumbers);
            first_map.put("app_id_number", getAppIdNumber(search_info.get("key"), mode, employee_id, tenant_id, status));
        } else if (mode.equals("my_todo") || mode.equals("have_done") || mode.equals("will_do")) {
            first_map.put("app_id_number", getAppIdNumber(search_info.get("key"), mode, employee_id, tenant_id, status));
        }
        return new Result(Code.GET_OK, first_map);
    }

    @GetMapping("/getFormData/{process_id}")
    public Result getFormData(@PathVariable Integer process_id) {
        return new Result(Code.GET_OK, processCenterService.getFormData(process_id));
    }

    @DeleteMapping("/deleteProcess/{process_id}")
    public Result deleteProcess(@PathVariable Integer process_id) {
        return new Result(Code.DELETE_OK, processCenterService.deleteProcess(process_id));
    }

    public Map<String, Map<String, Integer>> getAppIdNumber(String key, String mode, String employee_id, Integer tenant_id, String status) {
        Map<String, Map<String, Integer>> processIdNumber = new HashMap<>();
        List<Process> allProcess = processCenterService.getTargetProcessInfo(key, mode, employee_id, tenant_id, status, "all");
        for (Process process : allProcess) {
            String appId = process.getApp_id();
            String app_name = process.getApp_name();
            if(processIdNumber.containsKey(appId)) {
                Integer num = processIdNumber.get(appId).get(app_name);
                Map<String, Integer> value = processIdNumber.get(appId);
                value.put(app_name, num + 1);
            } else {
                Map<String, Integer> newMap = new HashMap<>();
                newMap.put(app_name, 1);
                processIdNumber.put(appId, newMap);
            }
        }
        return processIdNumber;
    }

    @PostMapping("/getOneTargetProcess/{process_id}")
    public Result getOneTargetProcess(@PathVariable Integer process_id, @RequestBody Map<String, String> map) {
        Integer operate_node = -1;
        if(map.containsKey("operate_node")) {
            operate_node = Integer.parseInt(map.get("operate_node"));
        }
        return new Result(Code.GET_OK, processCenterService.getOneTargetProcess(process_id, operate_node));
    }

    @PostMapping("/getNodeButtons")
    public Result getNodeButtons(@RequestBody Map<String, String> map, @RequestHeader Integer tenant_id) {
        Integer operate_node = -1;
        if(map.containsKey("operate_node")) {
            operate_node = Integer.parseInt(map.get("operate_node"));
        }
        return new Result(Code.GET_OK, processCenterService.getNodeButtons(Integer.parseInt(map.get("process_id")), map.get("mode"), tenant_id, operate_node));
    }

    @PostMapping("/setApprovalResult")
    public Result setApprovalResult(@RequestBody Map<String, String> map, @RequestHeader String employee_id) {
        Integer process_id = Integer.parseInt(map.get("process_id"));
        String res = processCenterService.setApprovalResult(process_id, map.get("result"), employee_id);
        return new Result(res.equals("") ? Code.UPDATE_OK : Code.UPDATE_ERR, res, res);
    }

    @GetMapping("/getSteps/{process_id}")
    public Result getSteps(@PathVariable Integer process_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, processCenterService.getSteps(process_id, tenant_id));
    }
}
